package authstates 
{
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import mediadisplay.ui.FunButton;
	import mediadisplay.ui.TextArea;
	/**
	 * ...
	 * @author Anders Figenschow selvfølgelig
	 */
	public class LoginWindow extends Sprite
	{
		[Embed(source='../../lib/mic.jpg')]
		public static var MicImage:Class;
		private var mic:Bitmap;
		
		[Embed(source='../../lib/title.jpg')]
		public static var Title:Class;
		private var title:Bitmap;
		private var username:TextArea;
		private var password:TextArea;
		private var _main:Main;
		
		public function LoginWindow(main:Main) 
		{
			_main = main;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			var fType:String = GradientType.LINEAR;
			var colors:Array = [ 0xff0000, 0 ];
			var alphas:Array = [ 1, 1 ];
			var ratios:Array = [ 0, 255 ];
			var matr:Matrix = new Matrix();
			matr.createGradientBox( stage.stageHeight, stage.stageWidth, 0, 0, 0 );
			matr.rotate(Math.PI/1.9);
			var sprMethod:String = SpreadMethod.PAD;
			
			graphics.clear();
			graphics.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod );
			graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			graphics.endFill();				
			
			var deco:Deco = new Deco();
			addChild(deco);
			
			mic = new MicImage() as Bitmap;
			addChild(mic);
			mic.blendMode = BlendMode.MULTIPLY;
			
			title = new Title() as Bitmap;
			addChild(title);
			title.x = -60;
			title.y = -100;
			title.blendMode = BlendMode.ADD;
			
			username = new TextArea("email address");
			addChild(username);
			username.y = 200;
			username.x = 140;
			username.rotationZ = 2;
			username.tf2.tabIndex = 1;
			
			password = new TextArea("password", true);
			addChild(password);
			password.y = 260;
			password.x = 140;
			password.rotationZ = 1;
			password.tf2.tabIndex = 2;
			
			var registerButton:FunButton = new FunButton("register!");
			addChild(registerButton);
			registerButton.x = 250;
			registerButton.y = 320;
			registerButton.rotationZ = 3;
			registerButton.tabIndex = 3;
			registerButton.addEventListener(MouseEvent.CLICK, registerUser);
			
			var submitButton:FunButton = new FunButton("log in!");
			addChild(submitButton);
			submitButton.x = 420;
			submitButton.y = 320;
			submitButton.rotationZ = -2;
			submitButton.tabIndex = 4;
			submitButton.addEventListener(MouseEvent.CLICK, tryToLogIn);
			
			deco.cacheAsBitmap = true;
		}
		
		public function setCredentials(user:String, pass:String):void {
			username.tf2.text = user;
			username.tf.text = "";
			password.tf2.text = pass;
			password.tf.text = "";
			
		}
		
		private function registerUser(e:MouseEvent):void 
		{
			username.setDescription("oh i'm sorry!");
			password.setDescription("you can't register yet!");
			username.tf2.text = "";
			password.tf2.text = "";
			username.focusOutHandler();
			password.focusOutHandler();
			
		}
		
		private function tryToLogIn(e:MouseEvent):void 
		{
			_main.getData(username.getValue(), password.getValue());
			username.setDescription("please wait.");
			password.setDescription("logging you in!");
			username.tf2.text = "";
			password.tf2.text = "";
			username.focusOutHandler();
			password.focusOutHandler();			
		}
		
		public function loginFailed():void {
			username.setDescription("wrong username...");
			password.setDescription("... or password :-(");
			username.tf2.text = "";
			password.tf2.text = "";
			username.focusOutHandler();
			password.focusOutHandler();
			
		}
		
		public function loginSucceeded():void {
			username.setDescription("Welcome!");
			password.setDescription("Let's party!");
			username.tf2.text = "";
			password.tf2.text = "";
			username.focusOutHandler();
			password.focusOutHandler();
			
		}
		
	}

}